{
  "info": {
    "_postman_id": "spay4-line-hmac-api",
    "name": "SPay4 LINE Ingress",
    "description": "SPay LINE 入款通知 的 Postman collection；契約以對接文件為準。Collection 認證為 No Auth：collection 層 pre-request script 以 CryptoJS 產生 X-Api-Key、X-Timestamp、X-Nonce、X-Signature，負向情境由 script 內的對照表觸發。Mutation 僅供手動逐筆執行；API key／secret 只填在本機 environment，不得匯出或提交。",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "auth": {
    "type": "noauth"
  },
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// 由 SPay 產生；請勿手改，更新時重新下載並匯入。",
          "// 簽章字串：METHOD\\nPATH\\nX-Timestamp\\nX-Nonce\\n 後接變數代換後的 UTF-8 body；PATH 為實際送出的 percent-encoded path。",
          "const credentials = {\"agentLine\":{\"apiKey\":\"agentLineApiKey\",\"secret\":\"agentLineApiSecret\"},\"supplierLine\":{\"apiKey\":\"supplierLineApiKey\",\"secret\":\"supplierLineApiSecret\"}};",
          "const scenarios = {",
          "  \"POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜正向\": {\"scenario\":\"positive\",\"credential\":\"agentLine\",\"status\":200,\"code\":2000},",
          "  \"POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜Supplier credential 正向\": {\"scenario\":\"supplierCredential\",\"credential\":\"supplierLine\",\"status\":200,\"code\":2000},",
          "  \"POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜冪等重送\": {\"scenario\":\"idempotent\",\"credential\":\"agentLine\",\"status\":200,\"code\":2000},",
          "  \"POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜欄位不合法\": {\"scenario\":\"invalidBody\",\"credential\":\"agentLine\",\"status\":400,\"code\":40017},",
          "  \"POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜錯簽章\": {\"scenario\":\"badSignature\",\"credential\":\"agentLine\",\"status\":401,\"code\":40000},",
          "  \"POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜過期 timestamp\": {\"scenario\":\"expiredTimestamp\",\"credential\":\"agentLine\",\"status\":401,\"code\":40000},",
          "  \"POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜nonce 重複\": {\"scenario\":\"duplicateNonce\",\"credential\":\"agentLine\",\"status\":401,\"code\":40000},",
          "};",
          "const entry = scenarios[pm.info.requestName];",
          "if (!entry) throw new Error(`「${pm.info.requestName}」不在 collection 的情境表內，請重新匯入 collection`);",
          "const credential = credentials[entry.credential];",
          "const apiKey = pm.variables.get(credential.apiKey);",
          "const secret = pm.variables.get(credential.secret);",
          "if (!apiKey || !secret) throw new Error(`請先在目前 environment 填入 ${credential.apiKey} 與 ${credential.secret}`);",
          "if (!/^https?:\\/\\/[^/]+$/.test(pm.variables.replaceIn('{{baseUrl}}'))) throw new Error('baseUrl 只能是 scheme://host[:port]，不可含 path 或結尾斜線');",
          "// path 變數值視為原文 identity，不先解碼；依 RFC 3986 pchar 規則（pchar 不編碼）產生 canonical segment，與 SPay 驗章的 PATH 相同。",
          "const segments = pm.request.url.path.map((segment) => encodeURIComponent(pm.variables.replaceIn(segment))",
          "  .replace(/%(?:24|26|2B|2C|3B|3D|3A|40)/g, (escaped) => decodeURIComponent(escaped)));",
          "pm.request.url.path = segments;",
          "const path = `/${segments.join('/')}`;",
          "const body = pm.request.body && pm.request.body.mode === 'raw' ? pm.variables.replaceIn(pm.request.body.raw) : '';",
          "let timestamp = Math.floor(Date.now() / 1000);",
          "if (entry.scenario === 'expiredTimestamp') timestamp -= 600;",
          "let nonce = pm.variables.replaceIn('{{$guid}}').toLowerCase();",
          "if (entry.scenario === 'duplicateNonce') {",
          "  nonce = pm.variables.get('fixedNonce') || '';",
          "  if (!/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(nonce)) throw new Error('nonce 重複情境需先在 environment 填入 fixedNonce（新的小寫 UUID v4）');",
          "}",
          "const message = `${pm.request.method}\\n${path}\\n${timestamp}\\n${nonce}\\n${body}`;",
          "let signature = CryptoJS.HmacSHA256(CryptoJS.enc.Utf8.parse(message), CryptoJS.enc.Utf8.parse(secret)).toString(CryptoJS.enc.Hex);",
          "if (entry.scenario === 'badSignature') signature = `${signature.slice(0, -1)}${signature.endsWith('0') ? '1' : '0'}`;",
          "pm.variables.set('hmacApiKey', apiKey);",
          "pm.variables.set('hmacTimestamp', String(timestamp));",
          "pm.variables.set('hmacNonce', nonce);",
          "pm.variables.set('hmacSignature', signature);",
          "pm.variables.set('hmacExpected', JSON.stringify(entry));"
        ]
      }
    },
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// 斷言 HTTP status 與對接文件「錯誤碼」章節的 code。",
          "const expected = JSON.parse(pm.variables.get('hmacExpected') || 'null');",
          "if (expected && expected.scenario === 'duplicateNonce' && pm.response.code !== 401) {",
          "  pm.test('nonce 重複：第一次送出不判定，請以同一 fixedNonce 再送一次', () => {});",
          "} else if (expected) {",
          "  pm.test(`HTTP ${expected.status}`, () => pm.response.to.have.status(expected.status));",
          "  pm.test(`code ${expected.code}`, () => pm.expect(pm.response.json().code).to.eql(expected.code));",
          "}"
        ]
      }
    }
  ],
  "item": [
    {
      "name": "Transactions",
      "item": [
        {
          "name": "POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence",
          "item": [
            {
              "name": "POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜正向",
              "request": {
                "method": "POST",
                "header": [
                  {
                    "key": "Content-Type",
                    "value": "application/json",
                    "type": "text"
                  },
                  {
                    "key": "X-Api-Key",
                    "value": "{{hmacApiKey}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Timestamp",
                    "value": "{{hmacTimestamp}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Nonce",
                    "value": "{{hmacNonce}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Signature",
                    "value": "{{hmacSignature}}",
                    "type": "text"
                  }
                ],
                "url": {
                  "raw": "{{baseUrl}}/v2/external/transactions",
                  "host": [
                    "{{baseUrl}}"
                  ],
                  "path": [
                    "v2",
                    "external",
                    "transactions"
                  ]
                },
                "description": "以 agentLineApiKey／agentLineApiSecret 簽章的正常請求。\n\n預期：HTTP 200、code 2000（對接文件「錯誤碼」章節）；Tests 會斷言兩者。\n\nMutation：僅可手動逐筆執行；不可使用 Collection Runner 自動鏈結。",
                "body": {
                  "mode": "raw",
                  "raw": "{\n  \"app_type\": \"LINE\",\n  \"account_id\": \"{{lineAccountId}}\",\n  \"message_id\": \"{{lineMessageId}}\",\n  \"timestamp\": \"{{lineTimestamp}}\",\n  \"msg_content\": \"{\\\"bank\\\":\\\"{{lineBank}}\\\",\\\"type\\\":\\\"IN\\\",\\\"transferInAccount\\\":\\\"{{transferInAccount}}\\\",\\\"transferOutAccount\\\":\\\"{{transferOutAccount}}\\\",\\\"transferInAmount\\\":\\\"{{transferInAmount}}\\\",\\\"transferInDate\\\":\\\"{{transferInDate}}\\\"}\"\n}",
                  "options": {
                    "raw": {
                      "language": "json"
                    }
                  }
                }
              },
              "response": []
            },
            {
              "name": "POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜Supplier credential 正向",
              "request": {
                "method": "POST",
                "header": [
                  {
                    "key": "Content-Type",
                    "value": "application/json",
                    "type": "text"
                  },
                  {
                    "key": "X-Api-Key",
                    "value": "{{hmacApiKey}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Timestamp",
                    "value": "{{hmacTimestamp}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Nonce",
                    "value": "{{hmacNonce}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Signature",
                    "value": "{{hmacSignature}}",
                    "type": "text"
                  }
                ],
                "url": {
                  "raw": "{{baseUrl}}/v2/external/transactions",
                  "host": [
                    "{{baseUrl}}"
                  ],
                  "path": [
                    "v2",
                    "external",
                    "transactions"
                  ]
                },
                "description": "改用 Supplier＋幣別＋提供方來源憑證（supplierLineApiKey／supplierLineApiSecret）送出，同一路由與簽章格式。\n\n預期：HTTP 200、code 2000（對接文件「錯誤碼」章節）；Tests 會斷言兩者。\n\nMutation：僅可手動逐筆執行；不可使用 Collection Runner 自動鏈結。",
                "body": {
                  "mode": "raw",
                  "raw": "{\n  \"app_type\": \"LINE\",\n  \"account_id\": \"{{lineAccountId}}\",\n  \"message_id\": \"{{lineMessageId}}\",\n  \"timestamp\": \"{{lineTimestamp}}\",\n  \"msg_content\": \"{\\\"bank\\\":\\\"{{lineBank}}\\\",\\\"type\\\":\\\"IN\\\",\\\"transferInAccount\\\":\\\"{{transferInAccount}}\\\",\\\"transferOutAccount\\\":\\\"{{transferOutAccount}}\\\",\\\"transferInAmount\\\":\\\"{{transferInAmount}}\\\",\\\"transferInDate\\\":\\\"{{transferInDate}}\\\"}\"\n}",
                  "options": {
                    "raw": {
                      "language": "json"
                    }
                  }
                }
              },
              "response": []
            },
            {
              "name": "POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜冪等重送",
              "request": {
                "method": "POST",
                "header": [
                  {
                    "key": "Content-Type",
                    "value": "application/json",
                    "type": "text"
                  },
                  {
                    "key": "X-Api-Key",
                    "value": "{{hmacApiKey}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Timestamp",
                    "value": "{{hmacTimestamp}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Nonce",
                    "value": "{{hmacNonce}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Signature",
                    "value": "{{hmacSignature}}",
                    "type": "text"
                  }
                ],
                "url": {
                  "raw": "{{baseUrl}}/v2/external/transactions",
                  "host": [
                    "{{baseUrl}}"
                  ],
                  "path": [
                    "v2",
                    "external",
                    "transactions"
                  ]
                },
                "description": "先送「正向」，再以同一 message_id 與相同內容送本筆：回 HTTP 200 與同一 transactionNo，不重複入款。\n\n預期：HTTP 200、code 2000（對接文件「錯誤碼」章節）；Tests 會斷言兩者。\n\nMutation：僅可手動逐筆執行；不可使用 Collection Runner 自動鏈結。",
                "body": {
                  "mode": "raw",
                  "raw": "{\n  \"app_type\": \"LINE\",\n  \"account_id\": \"{{lineAccountId}}\",\n  \"message_id\": \"{{lineMessageId}}\",\n  \"timestamp\": \"{{lineTimestamp}}\",\n  \"msg_content\": \"{\\\"bank\\\":\\\"{{lineBank}}\\\",\\\"type\\\":\\\"IN\\\",\\\"transferInAccount\\\":\\\"{{transferInAccount}}\\\",\\\"transferOutAccount\\\":\\\"{{transferOutAccount}}\\\",\\\"transferInAmount\\\":\\\"{{transferInAmount}}\\\",\\\"transferInDate\\\":\\\"{{transferInDate}}\\\"}\"\n}",
                  "options": {
                    "raw": {
                      "language": "json"
                    }
                  }
                }
              },
              "response": []
            },
            {
              "name": "POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜欄位不合法",
              "request": {
                "method": "POST",
                "header": [
                  {
                    "key": "Content-Type",
                    "value": "application/json",
                    "type": "text"
                  },
                  {
                    "key": "X-Api-Key",
                    "value": "{{hmacApiKey}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Timestamp",
                    "value": "{{hmacTimestamp}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Nonce",
                    "value": "{{hmacNonce}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Signature",
                    "value": "{{hmacSignature}}",
                    "type": "text"
                  }
                ],
                "url": {
                  "raw": "{{baseUrl}}/v2/external/transactions",
                  "host": [
                    "{{baseUrl}}"
                  ],
                  "path": [
                    "v2",
                    "external",
                    "transactions"
                  ]
                },
                "description": "簽章正確但 body 不合法：內層 type 只接受 IN。\n\n預期：HTTP 400、code 40017（對接文件「錯誤碼」章節）；Tests 會斷言兩者。\n\nMutation：僅可手動逐筆執行；不可使用 Collection Runner 自動鏈結。",
                "body": {
                  "mode": "raw",
                  "raw": "{\n  \"app_type\": \"LINE\",\n  \"account_id\": \"{{lineAccountId}}\",\n  \"message_id\": \"{{lineMessageId}}\",\n  \"timestamp\": \"{{lineTimestamp}}\",\n  \"msg_content\": \"{\\\"bank\\\":\\\"{{lineBank}}\\\",\\\"type\\\":\\\"OUT\\\",\\\"transferInAccount\\\":\\\"{{transferInAccount}}\\\",\\\"transferOutAccount\\\":\\\"{{transferOutAccount}}\\\",\\\"transferInAmount\\\":\\\"{{transferInAmount}}\\\",\\\"transferInDate\\\":\\\"{{transferInDate}}\\\"}\"\n}",
                  "options": {
                    "raw": {
                      "language": "json"
                    }
                  }
                }
              },
              "response": []
            },
            {
              "name": "POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜錯簽章",
              "request": {
                "method": "POST",
                "header": [
                  {
                    "key": "Content-Type",
                    "value": "application/json",
                    "type": "text"
                  },
                  {
                    "key": "X-Api-Key",
                    "value": "{{hmacApiKey}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Timestamp",
                    "value": "{{hmacTimestamp}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Nonce",
                    "value": "{{hmacNonce}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Signature",
                    "value": "{{hmacSignature}}",
                    "type": "text"
                  }
                ],
                "url": {
                  "raw": "{{baseUrl}}/v2/external/transactions",
                  "host": [
                    "{{baseUrl}}"
                  ],
                  "path": [
                    "v2",
                    "external",
                    "transactions"
                  ]
                },
                "description": "pre-request script 算出正確簽章後改掉最後一個十六進位字元。\n\n預期：HTTP 401、code 40000（對接文件「錯誤碼」章節）；Tests 會斷言兩者。\n\nMutation：僅可手動逐筆執行；不可使用 Collection Runner 自動鏈結。",
                "body": {
                  "mode": "raw",
                  "raw": "{\n  \"app_type\": \"LINE\",\n  \"account_id\": \"{{lineAccountId}}\",\n  \"message_id\": \"{{lineMessageId}}\",\n  \"timestamp\": \"{{lineTimestamp}}\",\n  \"msg_content\": \"{\\\"bank\\\":\\\"{{lineBank}}\\\",\\\"type\\\":\\\"IN\\\",\\\"transferInAccount\\\":\\\"{{transferInAccount}}\\\",\\\"transferOutAccount\\\":\\\"{{transferOutAccount}}\\\",\\\"transferInAmount\\\":\\\"{{transferInAmount}}\\\",\\\"transferInDate\\\":\\\"{{transferInDate}}\\\"}\"\n}",
                  "options": {
                    "raw": {
                      "language": "json"
                    }
                  }
                }
              },
              "response": []
            },
            {
              "name": "POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜過期 timestamp",
              "request": {
                "method": "POST",
                "header": [
                  {
                    "key": "Content-Type",
                    "value": "application/json",
                    "type": "text"
                  },
                  {
                    "key": "X-Api-Key",
                    "value": "{{hmacApiKey}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Timestamp",
                    "value": "{{hmacTimestamp}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Nonce",
                    "value": "{{hmacNonce}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Signature",
                    "value": "{{hmacSignature}}",
                    "type": "text"
                  }
                ],
                "url": {
                  "raw": "{{baseUrl}}/v2/external/transactions",
                  "host": [
                    "{{baseUrl}}"
                  ],
                  "path": [
                    "v2",
                    "external",
                    "transactions"
                  ]
                },
                "description": "X-Timestamp 為現在減 600 秒，超出 ±300 秒。\n\n預期：HTTP 401、code 40000（對接文件「錯誤碼」章節）；Tests 會斷言兩者。\n\nMutation：僅可手動逐筆執行；不可使用 Collection Runner 自動鏈結。",
                "body": {
                  "mode": "raw",
                  "raw": "{\n  \"app_type\": \"LINE\",\n  \"account_id\": \"{{lineAccountId}}\",\n  \"message_id\": \"{{lineMessageId}}\",\n  \"timestamp\": \"{{lineTimestamp}}\",\n  \"msg_content\": \"{\\\"bank\\\":\\\"{{lineBank}}\\\",\\\"type\\\":\\\"IN\\\",\\\"transferInAccount\\\":\\\"{{transferInAccount}}\\\",\\\"transferOutAccount\\\":\\\"{{transferOutAccount}}\\\",\\\"transferInAmount\\\":\\\"{{transferInAmount}}\\\",\\\"transferInDate\\\":\\\"{{transferInDate}}\\\"}\"\n}",
                  "options": {
                    "raw": {
                      "language": "json"
                    }
                  }
                }
              },
              "response": []
            },
            {
              "name": "POST /v2/external/transactions — 保存已驗證 LINE 入款 evidence｜nonce 重複",
              "request": {
                "method": "POST",
                "header": [
                  {
                    "key": "Content-Type",
                    "value": "application/json",
                    "type": "text"
                  },
                  {
                    "key": "X-Api-Key",
                    "value": "{{hmacApiKey}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Timestamp",
                    "value": "{{hmacTimestamp}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Nonce",
                    "value": "{{hmacNonce}}",
                    "type": "text"
                  },
                  {
                    "key": "X-Signature",
                    "value": "{{hmacSignature}}",
                    "type": "text"
                  }
                ],
                "url": {
                  "raw": "{{baseUrl}}/v2/external/transactions",
                  "host": [
                    "{{baseUrl}}"
                  ],
                  "path": [
                    "v2",
                    "external",
                    "transactions"
                  ]
                },
                "description": "先在目前 environment 把 fixedNonce 填為新的小寫 UUID v4；連送兩次，第一次為正常結果，第二次回 HTTP 401、code 40000。之後要再測須換新的 fixedNonce。\n\n預期：HTTP 401、code 40000（對接文件「錯誤碼」章節）；Tests 會斷言兩者。\n\nMutation：僅可手動逐筆執行；不可使用 Collection Runner 自動鏈結。",
                "body": {
                  "mode": "raw",
                  "raw": "{\n  \"app_type\": \"LINE\",\n  \"account_id\": \"{{lineAccountId}}\",\n  \"message_id\": \"{{lineMessageId}}\",\n  \"timestamp\": \"{{lineTimestamp}}\",\n  \"msg_content\": \"{\\\"bank\\\":\\\"{{lineBank}}\\\",\\\"type\\\":\\\"IN\\\",\\\"transferInAccount\\\":\\\"{{transferInAccount}}\\\",\\\"transferOutAccount\\\":\\\"{{transferOutAccount}}\\\",\\\"transferInAmount\\\":\\\"{{transferInAmount}}\\\",\\\"transferInDate\\\":\\\"{{transferInDate}}\\\"}\"\n}",
                  "options": {
                    "raw": {
                      "language": "json"
                    }
                  }
                }
              },
              "response": []
            }
          ]
        }
      ]
    }
  ],
  "variable": [
    {
      "key": "baseUrl",
      "value": ""
    }
  ]
}
